<?php
/**
 * @author fld 2021-07-16
 * @desc 查询数据业务类
 */
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class DataService
{
    /**
     * @author fld 2021-07-16
     * @desc 获取欢饮页数据
     * @return array
     */
    public function getWelcomeData(){
        $data = [];
        /* //总成功交易金额
        $data['all_success_amount'] = $this->getAllSuccessOrderAmount();
        //今日成功总金额
        $data['today_success_amount'] = $this->getTodaySuccessOrderAmount();
        //总成功订单数
        $data['all_success_count'] = $this->getAllSuccessOrderCount();
        //今日成功订单数
        $data['today_success_count'] = $this->getTodaySuccessOrderCount();

        //交易记录
        $data['order_list'] = $this->getOrderRecordList();
         */
        
        //总成功交易金额
        $data['all_success_amount'] = 0;
        //今日成功总金额
        $data['today_success_amount'] = 0;
        //总成功订单数
        $data['all_success_count'] = 0;
        //今日成功订单数
        $data['today_success_count'] = 0;
        
        //交易记录
        $data['order_list'] = [];
        return $data;
    }

    /**
     * @author fld 2021-07-16
     * @desc 获取总成功交易金额
     * @param $condition
     * @return float|int
     */
    public function getAllSuccessOrderAmount(){
        $all_amount = DB::table('order')->where('status', 1)->sum('cny_amount');
        return !empty($all_amount) ? floatval($all_amount) : 0;
    }

    /**
     * @author fld 2021-07-16
     * @desc 获取今日成功总金额
     * @param $condition
     * @return float|int
     */
    public function getTodaySuccessOrderAmount(){
        $condition = [];
        $begin_time = strtotime(date('Y-m-d'));
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        $condition[] = ['status','=',1];
        $condition[] = ['order_time', '>=' , $begin_time];
        $condition[] = ['order_time', '<=' , $end_time];
        $all_amount = DB::table('order')->where($condition)->sum('cny_amount');
        return !empty($all_amount) ? floatval($all_amount) : 0;
    }

    /**
     * @author fld 2021-07-16
     * @desc 获取总成功订单数
     * @param $condition
     * @return float|int
     */
    public function getAllSuccessOrderCount(){
        $all_count = DB::table('order')->where('status', 1)->count();
        return !empty($all_count) ? floatval($all_count) : 0;
    }

    /**
     * @author fld 2021-07-16
     * @desc 获取今日成功订单数
     * @param $condition
     * @return float|int
     */
    public function getTodaySuccessOrderCount(){
        $condition = [];
        $begin_time = strtotime(date('Y-m-d'));
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        $condition[] = ['status','=',1];
        $condition[] = ['order_time', '>=' , $begin_time];
        $condition[] = ['order_time', '<=' , $end_time];
        $all_count = DB::table('order')->where($condition)->count();
        return !empty($all_count) ? floatval($all_count) : 0;
    }

    public function getOrderRecordList(){
        $begin_time = strtotime(date("Y-m-d", strtotime('-30 day')));
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        $sql = "select FROM_UNIXTIME(o.order_time,'%Y-%m-%d') as order_time,sum(o.cny_amount) as all_amount,"
             . " sum(IF(o.`status`=1,o.cny_amount,0)) as success_amount,count(o.id) as all_count,count(IF(o.`status`=1,1,NULL)) as success_count"
             . " from ".env('DB_PREFIX')."order o "
             . " where o.order_time >= ".$begin_time." and o.order_time <= ".$end_time
             . " group by FROM_UNIXTIME(o.order_time,'%Y-%m-%d') "
             . " order by o.order_time ";
        $order_list = DB::select($sql);
        $order_list = objectToArray($order_list);
        $list = [];
        foreach($order_list as $k=>$v){
            $v['all_amount'] = floatval($v['all_amount']);
            $v['success_amount'] = floatval($v['success_amount']);
            $list[$v['order_time']] = $v;
        }
        $record_list = [];
        for($i = 29;$i >= 0;$i--){
            $date = date("Y-m-d", strtotime('-'.$i.' day'));
            if(isset($list[$date])){
                $record_list[$date] = $list[$date];
            }else{
                $record_list[$date] = [
                    'order_time' => $date,
                    'all_amount' => 0,
                    'success_amount' => 0,
                    'all_count' => 0,
                    'success_count' => 0,
                ];
            }
        }
        return $record_list;
    }
}